package com.premedsidekick;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class New_ExtracurricularRequirements extends Activity {
	SharedPreferences preferences;
	Editor editor;
	int flag = 0;
	ListView listview;
	Button button;
	Cursor cursor;
	Cursor cursor_clinical, cursor_community, cursor_research;
	Integer[] buttons = {
			R.id.extracurricular_requirements_button_1,
			R.id.extracurricular_requirements_button_2,
			R.id.extracurricular_requirements_button_3,
			R.id.extracurricular_requirements_button_4,
			R.id.extracurricular_requirements_button_5,
			R.id.extracurricular_requirements_button_6,
			
	};
	String[] possibilities = {
			"Clinical Exposure Hours",
			"Community Service Hours",
			"Research Hours",
			"Physician Shadowing Hours",
			"Oral Presentations",
			"Poster Presentations"
	};
	DefaultDatabaseAdapter dbHelper;
	DefaultCursorAdapter adapter;
	
	DefaultEntryDatabaseAdapter dbEntryHelper;
	DefaultEntryCursorAdapter entryAdapter;
	ExtracurricularRequirementsDatabaseManager manager;
	Context context;
	
	/*
	DefaultDatabaseAdapter clinical;
	DefaultDatabaseAdapter community;
	DefaultDatabaseAdapter research;
	DefaultCursorAdapter c_clinical;
	DefaultCursorAdapter c_community;
	DefaultCursorAdapter c_research;
	String CLINICAL = "clinical_exposure_table";
	String COMMUNITY = "community_service_table";
	String RESEARCH = "research_table";
	*/
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.extracurricular_requirements);
		manager = new ExtracurricularRequirementsDatabaseManager(this);
		listview = (ListView)findViewById(R.id.extracurricular_requirements_list_view);
		listview.setDivider(null);
		context = this;
		adapter = null;
		final TextView textview = (TextView)findViewById(R.id.ec_marker);
		final TextView t = (TextView)findViewById(R.id.ec_marker_left);
		/*
		clinical = new DefaultDatabaseAdapter(this, CLINICAL);
		clinical.open();
		cursor_clinical = clinical.getAll();
		community = new DefaultDatabaseAdapter(this, COMMUNITY);
		community.open();
		cursor_community = community.getAll();
		research = new DefaultDatabaseAdapter(this, RESEARCH);
		research.open();
		cursor_research = research.getAll();
		
		c_clinical = new DefaultCursorAdapter(context, R.layout.research_layout, cursor_clinical, clinical);
		c_community = new DefaultCursorAdapter(context, R.layout.research_layout, cursor_community, community);
		c_research = new DefaultCursorAdapter(context, R.layout.research_layout, cursor_research, research);
		*/
		for (int i = 0; i < 6; i++) {
			final int j = i;
			button = (Button)findViewById(buttons[i]);
			//System.out.println("Button: " + button);
			button.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					flag = j;
					if (j > 3)
						textview.setText(possibilities[j]);
					else
						textview.setText(possibilities[j]);
					
					/*
					if (flag == 0) {
						listview.setAdapter(c_clinical);
						cursor_clinical = clinical.getAll();
						//cursor_clinical.requery();
					}
					else if (flag == 1) {
						listview.setAdapter(c_community);
						cursor_community.requery();
					}
					else {
						listview.setAdapter(c_research);
						cursor_research.requery();
					}
					*/
					if (cursor != null)
						cursor.deactivate();
					//flag = j;
					//System.out.println("Test");
					if (flag < 4) {
						dbHelper = manager.getAdapter(j);
					//System.out.println("dbHelper: " + dbHelper);
					//System.out.println("Cursor: " + cursor);
						cursor = dbHelper.getAll();
						if (adapter == null)
							adapter = new DefaultCursorAdapter(context, R.layout.research_layout, cursor, dbHelper);
						adapter.initialize(dbHelper, cursor);
						listview.setAdapter(adapter);
					}
					else if (flag > 3) {
						dbEntryHelper = manager.getDefaultEntryAdapter(j);
						cursor = dbEntryHelper.getAll();
						if (entryAdapter == null)
							entryAdapter = new DefaultEntryCursorAdapter(context, cursor, dbEntryHelper);
						entryAdapter.initialize(dbEntryHelper, cursor);
						listview.setAdapter(entryAdapter);
					}
					
				}
				
			});
		}
		
		button = (Button)findViewById(R.id.extracurricular_requirements_add);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				/*
				if (flag == 0) {
					clinical.create("Insert name", "0");
					cursor_clinical.requery();
			
				}
				else if (flag == 1) {
					community.create("Insert name", "0");
					cursor_community.requery();
				}
				else if (flag == 2) {
					research.create("Insert name", "0");
					cursor_research.requery();
				}
				*/
				if (flag < 4) {
					if (dbHelper != null) {
						dbHelper.create("Insert name", "0");
						cursor.requery();
					}
				}
				else if (flag > 3) {
					if (dbEntryHelper != null) {
						dbEntryHelper.create("Insert name");
						cursor.requery();
					}
				}
					
				
			}
		});
		preferences = getSharedPreferences("user_settings", 0);
		editor = preferences.edit();
		int usages = preferences.getInt("usages", 0);
		if (usages < 2) {
			final int state = usages + 1;
			Dialog dialog = new Dialog(this);
			dialog.setCancelable(true);
			dialog.setTitle("Basic Instructions");
			dialog.setContentView(R.layout.maindialog);
			TextView tv = (TextView)dialog.findViewById(R.id.TextView01);
			String message = getResources().getString(R.string.instructions);
			CharSequence sequence = Html.fromHtml(message);
			tv.setText(sequence);
			dialog.setOnCancelListener(new OnCancelListener() {

				@Override
				public void onCancel(DialogInterface dialog) {
					editor.putInt("usages", state);
					editor.commit();
				}
				
			});
			dialog.show();
		}
	}
	
	public class ExtracurricularRequirementsDatabaseManager {
		DefaultDatabaseAdapter clinical;
		DefaultDatabaseAdapter community;
		DefaultDatabaseAdapter research;
		DefaultDatabaseAdapter physician;
		DefaultEntryDatabaseAdapter oral;
		DefaultEntryDatabaseAdapter poster;

		DefaultCursorAdapter c_clinical;
		DefaultCursorAdapter c_community;
		DefaultCursorAdapter c_research;
		DefaultCursorAdapter c_physician;
		DefaultEntryCursorAdapter c_oral;
		DefaultEntryCursorAdapter c_poster;
		
		
		Context context;
		String CLINICAL = "clinical_exposure_table";
		String COMMUNITY = "community_service_table";
		String RESEARCH = "research_table";
		String PHYSICIAN = "physician_table";
		String ORAL = "oral_table";
		String POSTER = "poster_table";
		
		ExtracurricularRequirementsDatabaseManager(Context context) {
			this.context = context;
			clinical = community = research = physician = null;
			oral = poster = null;
		}
		public DefaultEntryDatabaseAdapter getDefaultEntryAdapter(int i) {
			DefaultEntryDatabaseAdapter adapter = null;
			switch (i) {
			case 4:
				if (oral == null) {
					oral = new DefaultEntryDatabaseAdapter(context, ORAL);
					oral.open();
				}
				adapter = oral;
				break;
			case 5:
				if (poster == null) {
					poster = new DefaultEntryDatabaseAdapter(context, POSTER);
					poster.open();
				}
				adapter = poster;
				break;
			default:
				//System.out.println("Reached in error.");
			
			}
			return adapter;
		}
		
		public DefaultDatabaseAdapter getAdapter(int i) {
			DefaultDatabaseAdapter adapter = null;
			//System.out.println("Flag: " + i);
			switch (i) {
				case 0:
					if (clinical == null) {
						clinical = new DefaultDatabaseAdapter(context, CLINICAL);
						clinical.open();
					}
						
					adapter = clinical;
					break;
				case 1:
					if (community == null) {
						community = new DefaultDatabaseAdapter(context, COMMUNITY);
						community.open();
					}
					adapter = community;
					break;
				case 2:
					if (research == null) {
						research = new DefaultDatabaseAdapter(context, RESEARCH);
						research.open();
					}
					adapter = research;
					break;
				case 3:
					if (physician == null) {
						physician = new DefaultDatabaseAdapter(context, PHYSICIAN);
						physician.open();
					}
					adapter = physician;
				default:
					//System.out.println("Reached in error");
				
			}
			return adapter;
		}
		public void kill() {
			if (clinical != null)
				clinical.close();
			if (community != null)
				community.close();
			if (research != null)
				research.close();
			if (physician != null)
				physician.close();
			if (oral != null)
				oral.close();
			if (poster != null)
				poster.close();
		}
		
	}
	
	public void onDestroy() {
		super.onDestroy();
		/*
		cursor_clinical.close();
		cursor_community.close();
		cursor_research.close();
		clinical.close();
		community.close();
		research.close();
		*/
		if (cursor != null)
			cursor.close();
		if (dbHelper != null) 
			dbHelper.close();
		
		manager.kill();
	}
}
